const args = require('minimist')(process.argv.slice(2)) // node scripts/dev.js reactivity -f global
const { resolve } = require('path')
const {build} = require('esbuild')
const { watch } = require('fs')

const target = args._[0] || 'reactivity'
const format = args.f || 'global'

// 开发环境只打包某一个
const pkg = require(resolve(__dirname, `../packages/${target}/package.json`))

const outputFormat = format.startsWith('global') ? 'iife' : format === 'cjs' ? 'cjs':'esm'

const outfile = resolve(__dirname, `../packages/${target}/dist/${target}.${format}.js`)

build({
  entryPoints: [resolve(__dirname, `../packages/${target}/src/index.ts`)],
  outfile,
  bundle: true, // 把所有包全部达到一起
  sourcemap: true,
  format: outputFormat,
  globalName: pkg.buildOptions?.name,
  platform: format === 'cjs'? 'node': 'browser',
  watch: {
    onRebuild(error) {
      if (!error) console.log('rebuild...');
    }
  }
}).then(() => {
  console.log('watching...');
})